package io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class IOTest5 {
	public static void main(String[] args) throws IOException {
		File srcDir = new File("D:/root");
		if(!srcDir.exists()||!srcDir.isDirectory()){
			System.out.println("无法找到目标目录！");
			return;
		}
		File[] files = srcDir.listFiles();
		File toSrc = new File("D:/root/tomcat-logs");
		if(!toSrc.exists()||!toSrc.isDirectory()){
			toSrc.mkdir();
		}
		for(File f:files){
			if(f.isDirectory()||!f.getName().endsWith(".txt")){
				break;
			}
			try {
				String type = f.getName().replaceAll(".txt", ".java");
				StringBuilder sb =new StringBuilder();
				sb.append("D:/root/tomcat-logs/").append(type);
				FileInputStream ins = new FileInputStream(f);
				FileOutputStream ops = new FileOutputStream(sb.toString());
				byte[] buff = new byte[1024];
				int length = -1;
				try {
					while((length=ins.read(buff))!=-1){
						ops.write(buff, 0, length);
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				ins.close();
				ops.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
